<?php

$GLOBALS['d3download_tables'] = array(
	'broken' => array(
		'reportid' ,
		'lid' ,
		'sender' ,
		'ip' ,
	) ,
	'cat' => array(
		'cid' ,
		'pid' ,
		'title' ,
		'imgurl' ,
		'shotsdir' ,
		'cat_weight' ,
		'submit_message' ,
	) ,
	'downloads' => array(
		'lid' ,
		'cid' ,
		'title' ,
		'url' ,
		'filename' ,
		'ext' ,
		'homepage' ,
		'version' ,
		'size' ,
		'platform' ,
		'logourl' ,
		'description' ,
		'html' ,
		'smiley' ,
		'br' ,
		'xcode' ,
		'submitter' ,
		'mail' ,
		'date' ,
		'hits' ,
		'rating' ,
		'votes' ,
		'visible' ,
		'cancomment' ,
		'comments' ,
		'kanrisya'
	) ,
	'unapproval' => array(
		'requestid' ,
		'lid' ,
		'cid' ,
		'title' ,
		'url' ,
		'filename' ,
		'ext' ,
		'homepage' ,
		'version' ,
		'size' ,
		'platform' ,
		'logourl' ,
		'description' ,
		'html' ,
		'smiley' ,
		'br' ,
		'xcode' ,
		'submitter' ,
		'mail' ,
		'date' ,
		'visible' ,
		'cancomment' ,
		'notify' ,
		'kanrisya'
	) ,
	'downloads_history' => array(
		'id' ,
		'lid' ,
		'cid' ,
		'title' ,
		'url' ,
		'filename' ,
		'ext' ,
		'description' ,
		'date' ,
	) ,
	'user_access' => array(
		'cid' ,
		'uid' ,
		'groupid' ,
		'can_read' ,
		'can_post' ,
		'can_edit' ,
		'can_delete' ,
		'post_auto_approved' ,
		'edit_auto_approved' ,
		'html' ,
	) ,
	'votedata' => array(
		'ratingid' ,
		'lid' ,
		'ratinguser' ,
		'rating' ,
		'ratinghostname' ,
		'ratingtimestamp' ,
	) ,
) ;

if ( ! function_exists('d3download_import_errordie') ) {
	function d3download_import_errordie()
	{
		$db =& Database::getInstance() ;

		echo _MD_D3DOWNLOADS_SQLONIMPORT ;
		echo $db->logger->dumpQueries() ;
		exit ;
	}
}

if ( ! function_exists('d3download_import_from_mydownloads') ) {
	function d3download_import_from_mydownloads( $mydirname , $import_mid )
	{
		$myts =& MyTextSanitizer::getInstance() ;
		$db =& Database::getInstance() ;
		$import_mid = intval( $import_mid ) ;

		// get name of `contents` table 
		$module_handler =& xoops_gethandler( 'module' ) ;
		$module =& $module_handler->get( $import_mid ) ;
		//if( sizeof( $from_tables ) != 5 ) d3download_import_errordie() ;
		$target_dirname = $module->getVar('dirname') ;

		// broken 
		$from_table = $db->prefix( $target_dirname.'_broken' ) ;
		$to_table = $db->prefix( $mydirname.'_broken' ) ;
		$db->query( "DELETE FROM `$to_table`" ) ;
		$irs = $db->query( "INSERT INTO `$to_table` (reportid,lid,sender,ip) SELECT reportid,lid,sender,ip FROM `$from_table`" ) ;
		if( ! $irs ) d3download_import_errordie() ;

		// cat 
		$from_table = $db->prefix( $target_dirname.'_cat' ) ;
		$to_table = $db->prefix( $mydirname.'_cat' ) ;
		$db->query( "DELETE FROM `$to_table`" ) ;
		$irs = $db->query( "INSERT INTO `$to_table` (cid,pid,title,imgurl,cat_weight) SELECT cid,pid,title,imgurl,'0' FROM `$from_table`" ) ;
		if( ! $irs ) d3download_import_errordie() ;

		// user_access 
		$to_table = $db->prefix( $mydirname.'_user_access' ) ;
		$db->query( "DELETE FROM `$to_table`" ) ;

		// downloads_history 
		$to_table = $db->prefix( $mydirname.'_downloads_history' ) ;
		$db->query( "DELETE FROM `$to_table`" ) ;

		// downloads 
		$from_table = $db->prefix( $target_dirname.'_downloads' ) ;
		$to_table = $db->prefix( $mydirname.'_downloads' ) ;
		$db->query( "DELETE FROM `$to_table`" ) ;
		$irs = $db->query( "INSERT INTO `$to_table` (lid,cid,title,url,homepage,version,size,platform,logourl,description, html, smiley, br, xcode,submitter,mail,date,hits,rating,votes,visible,cancomment,comments,kanrisya) SELECT lid,cid,title,url,homepage,version,size,platform,logourl,'','0','1','1','1',submitter,'',date,hits,rating,votes,'1','1',comments,'' FROM `$from_table`" ) ;
		if( ! $irs ) d3download_import_errordie() ;

		// txt 
		$result = $db->query("SELECT lid, description FROM ".$db->prefix( $target_dirname."_text" )."");
		while( list( $id, $description ) = $db->fetchRow( $result ) ) {
			$lid = intval( $id );
			$body = mysql_real_escape_string( $myts->stripSlashesGPC( $description ) );
			$irs = $db->query( "UPDATE ".$db->prefix($mydirname."_downloads")." SET description = '".$body."' WHERE lid = '".$lid."'");
		}
		if( ! $irs ) d3download_import_errordie() ;

		// votedata 
		$from_table = $db->prefix( $target_dirname.'_votedata' ) ;
		$to_table = $db->prefix( $mydirname.'_votedata' ) ;
		$db->query( "DELETE FROM `$to_table`" ) ;
		$irs = $db->query( "INSERT INTO `$to_table` (lid,ratinguser,rating,ratinghostname,ratingtimestamp) SELECT lid,ratinguser,rating,ratinghostname,ratingtimestamp FROM `$from_table`" ) ;
		if( ! $irs ) d3download_import_errordie() ;

		// upload files delete
		d3download_delete_uploadfiles_for_import( $mydirname );
	}
}

if ( ! function_exists('d3download_import_from_d3download') ) {
	function d3download_import_from_d3download( $mydirname , $import_mid, $uploads_dir_error )
	{
		$db =& Database::getInstance() ;

		if( ! empty( $uploads_dir_error ) ){
			redirect_header( XOOPS_URL."/modules/$mydirname/admin/index.php?page=import" , 10 , _MD_D3DOWNLOADS_FILE_CONFIGERROR ) ;
			exit ;
		} else {
			$module_handler = & xoops_gethandler( 'module' ) ;
			$from_module =& $module_handler->get( $import_mid ) ;
			$from_dirname = $from_module->getVar('dirname') ;
			foreach( $GLOBALS['d3download_tables'] as $table_name => $columns ) {
				$to_table = $db->prefix( $mydirname.'_'.$table_name ) ;
				$from_table = $db->prefix( $from_dirname.'_'.$table_name ) ;
				$columns4sql = implode( ',' , $columns ) ;
				$db->query( "DELETE FROM `$to_table`" ) ;
				$irs = $db->query( "INSERT INTO `$to_table` ( $columns4sql ) SELECT $columns4sql FROM `$from_table`" ) ;
				if( ! $irs ) d3download_import_errordie() ;
			}
			// upload files delete
			d3download_delete_uploadfiles_for_import( $mydirname );
			// upload files copy
			d3download_copy_uploadfiles_for_import( $mydirname, $from_dirname );
			// convert url
			d3download_copy_converturl_for_import( $mydirname, $from_dirname );
		}
	}
}

if ( ! function_exists('d3download_cofyfiles_from_d3download') ) {
	function d3download_cofyfiles_from_d3download( $mydirname , $import_mid , $id )
	{
		$db =& Database::getInstance() ;

		$module_handler =& xoops_gethandler( 'module' ) ;
		$from_module =& $module_handler->get( $import_mid ) ;

		// downloads 
		$to_table = $db->prefix( $mydirname.'_downloads' ) ;
		$from_table = $db->prefix( $from_module->getVar('dirname').'_downloads' ) ;
		$columns4sql = implode( ',' , array_diff( $GLOBALS['d3download_tables']['downloads'] , array( 'lid' , 'cid') ) ) ;
		$irs = $db->query( "INSERT INTO `$to_table` ($columns4sql,cid) SELECT $columns4sql,0 FROM `$from_table` WHERE lid=".intval($id) ) ;
		if( ! $irs ) d3download_import_errordie() ;

		// votedata 
		$new_content_id = $db->getInsertId() ;
		$to_table = $db->prefix( $mydirname.'_votedata' ) ;
		$from_table = $db->prefix( $from_module->getVar('dirname').'_votedata' ) ;
		$columns4sql = implode( ',' , array_diff( $GLOBALS['d3download_tables']['votedata'] , array( 'ratingid' , 'lid' ) ) ) ;
		$irs = $db->query( "INSERT INTO `$to_table` ($columns4sql,lid) SELECT $columns4sql,$new_content_id FROM `$from_table` WHERE lid=".intval($id) ) ;
		if( ! $irs ) d3download_import_errordie() ;
	}
}

if ( ! function_exists('d3download_delete_uploadfiles_for_import') ) {
	function d3download_delete_uploadfiles_for_import( $mydirname )
	{
		$uploads_dir = XOOPS_TRUST_PATH.'/uploads/'.$mydirname ;
		$site_salt = substr( md5( XOOPS_URL ) , -4 ) ;
		if( $handler = @opendir( $uploads_dir . '/' ) ) {
			while( ( $file = readdir( $handler ) ) !== false ) {
				$file_path = $uploads_dir . '/' . $file ;
				if ( is_file( $file_path ) && strstr( $file , $site_salt ) ){
					@unlink( $file_path ) or die("File delete error ". $file );
				}
			}
		}
		closedir( $handler ) ;
	}
}

if ( ! function_exists('d3download_copy_uploadfiles_for_import') ) {
	function d3download_copy_uploadfiles_for_import( $mydirname, $from_dirname )
	{
		$from_dir = XOOPS_TRUST_PATH.'/uploads/'.$from_dirname ;
		$uploads_dir = XOOPS_TRUST_PATH.'/uploads/'.$mydirname ;

		$safe_mode = ini_get( "safe_mode" ) ;
		if( ! is_dir( $uploads_dir ) ) {
			if( $safe_mode ) {
				redirect_header( XOOPS_URL."/modules/$mydirname/admin/index.php?page=import" , 3 , _MD_D3DOWNLOADS_FILE_NO_IMPORT ) ;
				exit ;
			}
			$mrs = mkdir( $uploads_dir , 0777 ) ;
			if( ! $mrs ) {
				redirect_header( XOOPS_URL."/modules/$mydirname/admin/index.php?page=import" , 3 , _MD_D3DOWNLOADS_FILE_NO_IMPORT ) ;
				exit ;
			} else @chmod( $uploads_dir , 0777 ) ;
		}
		if( ! is_writable( $uploads_dir ) || ! is_readable( $uploads_dir ) ) {
			$mrs = chmod( $uploads_dir , 0777 ) ;
			if( ! $mrs ) {
				redirect_header( XOOPS_URL."/modules/$mydirname/admin/index.php?page=import" , 3 , _MD_D3DOWNLOADS_FILE_NO_IMPORT ) ;
				exit ;
			}
		}

		if( empty( $error ) ){
			$site_salt = substr( md5( XOOPS_URL ) , -4 ) ;
			if( $handler = @opendir( $from_dir . '/' ) ) {
				while( ( $file = readdir( $handler ) ) !== false ) {
					$from_file = $from_dir . '/' . $file ;
					$new_file = $uploads_dir . '/' . $file ;
					if ( strstr( $from_file , $site_salt ) ){
						if ( ! copy( $from_file, $new_file ) ) {
							redirect_header( XOOPS_URL."/modules/$mydirname/admin/index.php?page=import" , 3 , _MD_D3DOWNLOADS_FILE_NO_IMPORT ) ;
							exit ;
						}
					}
				}
			}
			closedir( $handler ) ;
		}
	}
}

if ( ! function_exists('d3download_copy_converturl_for_import') ) {
	function d3download_copy_converturl_for_import( $mydirname, $from_dirname )
	{
		$myts =& MyTextSanitizer::getInstance() ;
		$db =& Database::getInstance() ;

		// upload url 
		$searches = XOOPS_TRUST_PATH.'/uploads/'.$from_dirname ;
		$replacements = XOOPS_TRUST_PATH.'/uploads/'.$mydirname ;

		$result = $db->query("SELECT lid, url FROM ".$db->prefix( $mydirname."_downloads" )."");
		while( list( $id, $url ) = $db->fetchRow( $result ) ) {
			$lid = intval( $id );
			$new_url = str_replace( $searches , $replacements , $url ) ;
			$new_url4sql = mysql_real_escape_string( $myts->stripSlashesGPC( $new_url ) );
			$irs = $db->query( "UPDATE ".$db->prefix($mydirname."_downloads")." SET url = '".$new_url4sql."' WHERE lid = '".$lid."'");
		}
		if( ! $irs ) d3download_import_errordie() ;

		$result = $db->query("SELECT lid, url FROM ".$db->prefix( $mydirname."_downloads_history" )."");
		while( list( $id, $url ) = $db->fetchRow( $result ) ) {
			$lid = intval( $id );
			$new_url = str_replace( $searches , $replacements , $url ) ;
			$new_url4sql = mysql_real_escape_string( $myts->stripSlashesGPC( $new_url ) );
			$irs = $db->query( "UPDATE ".$db->prefix($mydirname."_downloads_history")." SET url = '".$new_url4sql."' WHERE lid = '".$lid."'");
		}
		if( ! $irs ) d3download_import_errordie() ;

		$result = $db->query("SELECT lid, url FROM ".$db->prefix( $mydirname."_unapproval" )."");
		while( list( $id, $url ) = $db->fetchRow( $result ) ) {
			$lid = intval( $id );
			$new_url = str_replace( $searches , $replacements , $url ) ;
			$new_url4sql = mysql_real_escape_string( $myts->stripSlashesGPC( $new_url ) );
			$irs = $db->query( "UPDATE ".$db->prefix($mydirname."_unapproval")." SET url = '".$new_url4sql."' WHERE lid = '".$lid."'");
		}
		if( ! $irs ) d3download_import_errordie() ;
	}
}

?>